/*
* File: logfile.c
* Version: 1.0
* Last modified on Fri Mars 07 2014 by group 5
* Members:
* Johan Forsblom, Johanna Carlsson, Dragana Todorovic, 
* Hampus Herbertsson, Robin Henriksson
* -----------------------------------------------------
* This file implements the logfile.h interface.
*/

#include <stdio.h>
#include <string.h>
#include "logfile.h"
#include "configFile.h"


/* Function: getLogConnectingClient
* usage: getLogConnectingClient(logFile, client);
* ----------------------------------------------------------------
* This function gets the connected client ip/hostname.
*/
void getLogConnectingClient(logFileT *logFile, char *client)
{
	sprintf(logFile->connectingClient, "%s", client);
}


/* Function: getLogTimeStamp
* usage: getLogTimeStamp(logFile, timeStamp);
* ----------------------------------------------------------------
* This function gets the current server time. 
*/
void getLogTimeStamp(logFileT *logFile, char *timeStamp)
{
	int length;
	length = strlen(timeStamp);
	/* The timestamp contains \n\r in the end, so we remove them.*/
	timeStamp[length -2] = '\0'; 
	sprintf(logFile->timeStamp, "%s", timeStamp);
}


/* Function: getLogCommandAndURI
* usage: getLogCommandAndURI(logFile, command, fileName);
* ----------------------------------------------------------------
* This function gets the webb browser command and URI request.
*/
void getLogCommandAndURI(logFileT *logFile, char *command, char *fileName)
{
	sprintf(logFile->command,"\"%s %s", command, fileName);
}


/* Function: getLogResponse
* usage: getLogResponse(logFile, response);
* ----------------------------------------------------------------
* This function gets the http response.
*/
void getLogResponse(logFileT *logFile,char *response)
{
	sprintf(logFile->response, "%s", response);
}


/* Function: getLogfileSize
* usage: getLogfileSize(logFile, bytes);
* ----------------------------------------------------------------
* This function gets the file size.
*/
void getLogfileSize(logFileT *logFile, int bytes)
{
	sprintf(logFile->bytes, "%d", bytes);
}


/* Function: writeLogFile
* usage: writeLogFile(logFile);
* ----------------------------------------------------------------
* This function writes the logfile to disk.
*/
void writeLogFile(logFileT *logFile)
{
	FILE *file;
	char logFileFolder[255];
	getLogFileFolder(logFileFolder);
	if(logFileFolder[0] != '/'){
		strcat(logFileFolder, "serverLogFile.txt");
	}
	else {
		sprintf(logFileFolder, "serverLogFile.txt");
	}
	file = fopen(logFileFolder, "a+");
	if(file == NULL){
		printf("Error could not open: %s  for writing\n", logFileFolder);
	} 
	else {
		fprintf(file, "%s [%s]\n%s %s %s\n\n", logFile->connectingClient,
			logFile->timeStamp, logFile->command, logFile->response, logFile->bytes); 
		fclose(file);
	}
}


/* Function: clearLogStruct
* usage: clearLogStruct(logFile);
* ----------------------------------------------------------------
* This function clears the log struct.
*/
void clearLogStruct(logFileT *logFile)
{
	sprintf(logFile->bytes, " ");
	sprintf(logFile->command, " ");
	sprintf(logFile->connectingClient, " ");
	sprintf(logFile->response, " ");
	sprintf(logFile->timeStamp, " ");
	sprintf(logFile->URI, " ");
}